#!/usr/bin/make -f

pkg_name = temurin-19-jdk
priority = 1911
jvm_tools = jar jarsigner java javac javadoc javap jcmd jconsole jdb jdeprscan jdeps jfr jhsdb jimage jinfo jlink jmap jmod jpackage jps jrunscript jshell jstack jstat jstatd jwebserver keytool rmiregistry serialver jexec jspawnhelper
amd64_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_x64_linux_hotspot_19.0.2_7.tar.gz
amd64_checksum = 3a3ba7a3f8c3a5999e2c91ea1dca843435a0d1c43737bd2f6822b2f02fc52165
arm64_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_aarch64_linux_hotspot_19.0.2_7.tar.gz
arm64_checksum = 1c4be9aa173cb0deb0d215643d9509c8900e5497290b29eee4bee335fa57984f
armhf_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_arm_linux_hotspot_19.0.2_7.tar.gz
armhf_checksum = 6a51cb3868b5a3b81848a0d276267230ff3f8639f20ba9ae9ef1d386440bf1fd
ppc64el_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_ppc64le_linux_hotspot_19.0.2_7.tar.gz
ppc64el_checksum = 173d1256dfb9d13d309b5390e6bdf72d143b512201b0868f9d349d5ed3d64072
s390x_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2%2B7/OpenJDK19U-jdk_s390x_linux_hotspot_19.0.2_7.tar.gz
s390x_checksum = f2512f9a8e9847dd5d3557c39b485a8e7a1ef37b601dcbcb748d22e49f44815c

d = debian/$(pkg_name)
jvm_home = usr/lib/jvm
jvm_dir = $(pkg_name)-$(DEB_HOST_ARCH)
pkg_alias = $(pkg_name)-$(DEB_HOST_ARCH)

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_strip:
override_dh_auto_install:
	# Download and verify checksum of JDK
	wget --progress=dot:mega -O jdk.tar.gz $($(DEB_HOST_ARCH)_tarball_url)
	echo '$($(DEB_HOST_ARCH)_checksum) jdk.tar.gz' > jdk.tar.gz.sha256.txt
	sha256sum -c jdk.tar.gz.sha256.txt

	# Substitute variables.
	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/jinfo.in > debian/jinfo

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/postinst.in > debian/postinst

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/prerm.in > debian/prerm

	# Unpack JDK.
	dh_installdirs \
		$(jvm_home) \
		$(jvm_home)/$(jvm_dir)
	tar --strip-components=1 -C "$(d)/$(jvm_home)/$(jvm_dir)" -xf jdk.tar.gz

	# Add jinfo file (alternatives support).
	cp debian/jinfo "$(d)/$(jvm_home)/.$(pkg_alias).jinfo"

	# Strip bundled Freetype and use OS package instead.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/libfreetype.so"

	# Replace bundled cacerts and redirect to adoptium-ca-certificates.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"
	ln -s /etc/ssl/certs/adoptium/cacerts "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"

	# Ensure src.zip is present in the root folder of the JDK.
	if [ ! -f "$(d)/$(jvm_home)/$(jvm_dir)/src.zip" ]; then \
		if [ ! -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/src.zip" ] ; then \
			echo "Missing src.zip in binary distribution"; \
			exit 1; \
		fi; \
		ln -s lib/src.zip "$(d)/$(jvm_home)/$(jvm_dir)/src.zip"; \
	fi;

override_dh_gencontrol:
	dh_gencontrol --
